package saas.consumer.service.biz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import saas.consumer.service.remote.OmsBaseInfoFeignClient;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @title: AopTest
 * @description:
 * @author: guan.xin
 * @create: 2021-11-17 16:46
 * @since: JDK8
 */
@Slf4j
@Component
public class BizService extends BaseBizService {

    private static final Map<String, TenantConversionRelation> map1 = new HashMap<>(4096);

    @Resource
    private OmsBaseInfoFeignClient omsBaseInfoFeignClient;

    public Map<String, TenantConversionRelation> getMap() {
        this.loadAllOwnerStockSaasInfo();
        return map1;
    }

    private void loadAllOwnerStockSaasInfo() {
        JSONObject allOwnerStockSaasInfo = omsBaseInfoFeignClient.findAllOwnerStockSaasInfo();

        if (allOwnerStockSaasInfo != null && !allOwnerStockSaasInfo.isEmpty()) {
            Boolean isSuccess = allOwnerStockSaasInfo.getBoolean("isSuccess");
            if (isSuccess != null && isSuccess) {
                JSONArray data = allOwnerStockSaasInfo.getJSONArray("data");
                if (data != null && !data.isEmpty()) {
                    data.forEach(e -> {
                        JSONObject o = JSON.parseObject(JSON.toJSONString(e));
                        TenantConversionRelation tenantConversionRelation = TenantConversionRelation.builder();
                        map1.put(o.getString("code"), tenantConversionRelation
                                .setCode(o.getString("code"))
                                .setOmsTenantCode(o.getString("saasTenantCode"))
                                .setMpTenantCode(o.getString("middleTenantCode")));
                    });
                } else {
                    log.error("data is null，加载全部库存主体映射关系数据为空");
                }
            } else {
                log.error("isSuccess is false，加载全部库存主体映射关系数据为空");
            }
        } else {
            log.error("allShopSaasInfo is null，加载全部库存主体映射关系数据为空");
        }
    }
}
